﻿using UnityEngine;
using Unity.Entities;
using Unity.Rendering;

public class AnimationDataHolderAuthoring : MonoBehaviour
{
    public AnimationDatalListSO animationDatalListSO;
    public Material defaultMaterial;

    public class Baker : Baker<AnimationDataHolderAuthoring>
    {
        public override void Bake(AnimationDataHolderAuthoring authoring)
        {
            Entity entity = GetEntity(TransformUsageFlags.Dynamic);
            AnimationDataHolder animationDataHolder = new AnimationDataHolder();

            //EntitiesGraphicsSystem entitiesGraphicsSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<EntitiesGraphicsSystem>();

            //构建Blob 数组
            //BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp);
            //ref BlobArray<AnimationData> animationDataBlobArray = ref blobBuilder.ConstructRoot<BlobArray<AnimationData>>();//忘记使用 ref 关键字可能导致使用的是数据的副本


            //BlobBuilderArray<AnimationData> animationdAtaBlobArray
            //    = blobBuilder.Allocate<AnimationData>(ref animationDataBlobArray, System.Enum.GetValues(typeof(AnimationDataSO.AnimationType)).Length);

            int index = 0;
            //Blob学习
            foreach (AnimationDataSO.AnimationType animationType in System.Enum.GetValues(typeof(AnimationDataSO.AnimationType)))
            {
                AnimationDataSO animationDataSO = authoring.animationDatalListSO.GetAnimationDataSO(animationType);
                //BlobBuilderArray<BatchMeshID> blobBuilderArray =
                //  blobBuilder.Allocate<BatchMeshID>(ref animationdAtaBlobArray[index].batchMeshIdBlobArray, animationDataSO.meshArray.Length);

                //animationdAtaBlobArray[index].frameTimerMax = animationDataSO.frameTimerMax;
                //animationdAtaBlobArray[index].frameMax = animationDataSO.meshArray.Length;

                for (int i = 0; i < animationDataSO.meshArray.Length; i++)
                {
                    Mesh mesh = animationDataSO.meshArray[i];
                    Entity additionalEntity = CreateAdditionalEntity(TransformUsageFlags.None, true);
                    AddComponent(additionalEntity, new MaterialMeshInfo());
                    AddComponent(additionalEntity, new RenderMeshUnmanaged
                    {
                        materialForSubMesh = authoring.defaultMaterial,
                        mesh = mesh,
                    });
                    AddComponent(additionalEntity, new AnimationDataHolderSubEntity
                    {
                        animationType = animationType,
                        meshIndex = i,
                        //blobBuilderArray[i] = entitiesGraphicsSystem.RegisterMesh(mesh);
                    });
                    index++;
                }

                //animationDataHolder.animationDataBlobArrayBlobAssetReference =
                //    blobBuilder.CreateBlobAssetReference<BlobArray<AnimationData>>(Allocator.Persistent);

                ////释放blobBuilder构建器(忘记了会导致内存泄漏)
                //blobBuilder.Dispose();

                ////确保被使用了一次 以防止被释放掉(如果忘记调用 可能会导致一些未分配的引用被自动释放)
                //AddBlobAsset(ref animationDataHolder.animationDataBlobArrayBlobAssetReference, out Unity.Entities.Hash128 objectHash);

            }
            AddComponent(entity, new AnimationDataHolderObjectData
            {
                animationDataListSO = authoring.animationDatalListSO,
            });


            AddComponent(entity, animationDataHolder);
        }
    }
}

public struct AnimationDataHolderObjectData : IComponentData
{
    public UnityObjectRef<AnimationDatalListSO> animationDataListSO;
}


public struct AnimationDataHolderSubEntity : IComponentData
{
    public AnimationDataSO.AnimationType animationType;
    public int meshIndex;
}

public struct AnimationDataHolder : IComponentData
{
    public BlobAssetReference<BlobArray<AnimationData>> animationDataBlobArrayBlobAssetReference;
}

public struct AnimationData
{
    public float frameTimerMax;
    public int frameMax;
    public BlobArray<int> intMeshIdBlobArray;
}
